﻿using EnvDTE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace LuomgAddin
{
    class DeleteNullCommand: DTECommand
    {
        public DeleteNullCommand()
        {
            this.Text = "删除空行";
            this.ToolTip = "删除当前文档空行";
        }
        protected override void IntializeCore()
        {
            base.IntializeCore();
            {
                var events = this.DTE.Events.get_DocumentEvents() as DocumentEvents;
                events.DocumentSaved += OnDocumentSaved;
            }
        }
        private void OnDocumentSaved(Document document)
        {
            this.TryAddFileHeader(document, true);
        }
        private void TryAddFileHeader(Document activeDoc, bool alert)
        {
            var selection = activeDoc.Selection as TextSelection;
            if (selection != null)
            {
                var ac = selection.ActivePoint.AbsoluteCharOffset;
                selection.SelectAll();
                var t = selection.Text;
                selection.MoveToAbsoluteOffset(ac);


                Match m = Regex.Match(t, "");

          
                selection.StartOfDocument();

                selection.StartOfDocument();
                selection.FindText("$end$");
                selection.Delete();
            }
        }
        protected override vsCommandStatus QueryStatusCore()
        {
            if (DTE.ActiveDocument != null) { return base.QueryStatusCore(); }

            return vsCommandStatus.vsCommandStatusSupported;
        }
        protected override void ExecuteCore()
        {
            var activeDoc = DTE.ActiveDocument;
            if (activeDoc != null)
            {
                this.TryAddFileHeader(activeDoc, false);
            }
        }
    }
}
